package com.yunc.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.CommonController;
import com.yunc.common.aspect.BaseLog;
import com.yunc.common.utils.EasyPoiUtil;
import com.yunc.common.utils.R;
import com.yunc.modules.sys.entity.SysRole;
import com.yunc.modules.sys.entity.SysRoleMenu;
import com.yunc.modules.sys.service.ISysRoleMenuService;
import com.yunc.modules.sys.service.ISysRoleService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 系统角色
 *
 * @author congcong tao
 * @date 2018/5/14 16:14
 */
@RestController
@RequestMapping("sysRole")
public class SysRoleController extends CommonController {

    private static final Logger logger = LogManager.getLogger(SysRoleController.class);

    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @BaseLog("角色管理")
    @GetMapping("/list")
    public List<SysRole> list(SysRole sysRole){
        QueryWrapper<SysRole> ew = new QueryWrapper<>(sysRole);
        List<SysRole> sysRoleList = sysRoleService.list(ew);
        return sysRoleList;
    }
    
    @GetMapping("/page")
    public R page(Page<SysRole> page, SysRole sysRole){
        sysRoleService.page(page, new QueryWrapper<>(sysRole));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    /**
     * 菜单信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        SysRole role = sysRoleService.getById(id);
        return R.ok().put("role", role);
    }

    /**
     * 保存
     */
    @BaseLog("角色管理")
    @PostMapping("/save")
    @RequiresPermissions("sys:user:save")
    public R save(@RequestBody SysRole role){
        role.setCreateUser(getUserId());
        role.setCreateTime(new Date());
        boolean flag = sysRoleService.save(role);
        if (flag) {
            // 修改权限
            boolean roleMenuFlag = sysRoleMenuService.insertList(role.getId(), role.getMenuIds());
            if (roleMenuFlag) {
                return R.ok();
            }
        }
        return R.error();
    }

    /**
     * 修改
     */
    @BaseLog("角色管理")
    @PutMapping(value = "/update")
    @RequiresPermissions("sys:role:update")
    public R update(@RequestBody SysRole role){
        Long roleId = role.getId();
        boolean flag = sysRoleService.updateById(role);
        if (flag) {
            // 删除权限
            SysRoleMenu record = new SysRoleMenu();
            record.setRoleId(roleId);
            boolean removeFlag = sysRoleMenuService.remove(new QueryWrapper<>(record));
            boolean roleMenuFlag = sysRoleMenuService.insertList(role.getId(), role.getMenuIds());
            if (removeFlag && roleMenuFlag) {
                return R.ok();
            }
        }
        return R.error();
    }

    @RequestMapping("export")
    public void export(HttpServletResponse response, SysRole sysRole){
        QueryWrapper<SysRole> ew = new QueryWrapper<>(sysRole);
        List<SysRole> sysRoleList = sysRoleService.list(ew);
        // 导出操作
        EasyPoiUtil.exportExcel(sysRoleList, "角色", "角色", SysRole.class, "角色.xls", response);
    }
}
